﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Rcompany.IPaymentApproval;
using Rcompany.Model;
using Rcompany.PaymentApprovalDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Rcompany.PaymentApprovals
{
    public class PaymentApprovalStore : ApplicationService, IPaymentApprovals
    {
        public readonly IRepository<PaymentApproval, Guid> payments;
        public readonly IRepository<PaymentManagement, Guid> managements;
        public PaymentApprovalStore(IRepository<PaymentApproval, Guid> _payments, IRepository<PaymentManagement, Guid> _managements)
        {
            payments = _payments;
            managements = _managements;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PaymentApprovalDto> Add(CreateUpdateApprovalDto m)
        {
            var create = await payments.InsertAsync(ObjectMapper.Map<CreateUpdateApprovalDto, PaymentApproval>(m));
            return ObjectMapper.Map<PaymentApproval, PaymentApprovalDto>(create);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PaymentApprovalDto>> Show(string name="")
        {
            var query = from a in payments
                        join b in managements
                        on a.Management equals b.Id
                        select new PaymentApprovalDto
                        {
                            Id = b.Id,
                            Management = a.Management,
                            Account = b.Account,
                            Amount = b.Amount,
                            ApplyPeople = b.ApplyPeople,
                            Approver = a.Approver,
                            CreatingTime = b.CreatingTime,
                            CreationTime = b.CreationTime,
                            CreatorId = b.CreatorId,
                            DatePayment = b.DatePayment,
                            Describe = b.Describe,
                            LastModificationTime = b.LastModificationTime,
                            LastModifierId = b.LastModifierId,
                            MethodPayment = b.MethodPayment,
                            Note = b.Note,
                            OpenAccount = b.OpenAccount,
                            Payobject = b.Payobject,
                            Status = a.Status,
                            Title = b.Title,

                        };
            if (name=="")
            {
                return await query.ToListAsync();
            }
            else
            {
                return await query.Where(x=>x.Title.Equals(name)).ToListAsync();
            }
        }
    }
}
